// pages/movies/more-movie/more-movie.js
import util from "../../../asset/js/util";

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navigateTitle:"",
    movies:"",
    // 获取当前接口url
    requestUrl:"",
    // 滚动刷新次数累加
    totalCount:0,
    isEmpty:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var movieListTitle = options.movieListTitle;
    var dataUrl = "";
    // console.log(options);
    // this.data.navigateTitle = movieListTitle;
    this.setData({
      navigateTitle:movieListTitle
    });
    switch (movieListTitle) {
      case "正在热映":
        dataUrl = app.globalData.doubanBase + "/v2/movie/in_theaters";
        break;
      case "即将上映":
        dataUrl = app.globalData.doubanBase + "/v2/movie/coming_soon";
        break;
      case "豆瓣Top250":
        dataUrl = app.globalData.doubanBase + "/v2/movie/top250";
        break;
    }
    this.setData({
      requestUrl:dataUrl
    });
    util.http(dataUrl, this.processDoubanData);
  },
  /* 下拉刷新和加载更多同时存在的方法 */
  /* onScrollLower:function(event){
    var nextUrl=this.data.requestUrl+"?start="+this.data.totalCount+"&count=20";
    util.http(nextUrl, this.processDoubanData);
    console.log("上滑加载,nextUrl:"+nextUrl);
    wx.showNavigationBarLoading();
  }, */
  /* 设置movies的数据 */
  processDoubanData: function (moviesDouban) {
    var movies=[];
    for(var index in moviesDouban.subjects){
      var subject=moviesDouban.subjects[index];
      var title=subject.title;
      var temp={
          stars:util.convertToStarsArray(subject.rating.stars),
          title:title,
          average:subject.rating.average,
          coverageUrl:subject.images.large,
          movieId:subject.id
      }
      movies.push(temp);
    }
    var totalMovies={}
    // 加载数据后，自增20
    var i=this.data.totalCount;
    i+=20;
    // 如果要绑定新加载的数组，要和旧有的数组绑定在一起
    if(!this.data.isEmpty){
      totalMovies=this.data.movies.concat(movies);
    }else{
      totalMovies=movies;
      this.setData({
        isEmpty:false
      });
    }
    this.setData({
      movies:totalMovies,
      totalCount:i
    });
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.setNavigationBarTitle({
      title: this.data.navigateTitle
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var requestUrl=this.data.requestUrl+"?star=0&count=20";
    this.setData({
      movies:{},
      isEmpty:true,
      totalCount:0
    });
    util.http(requestUrl, this.processDoubanData);
    wx.showNavigationBarLoading();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var nextUrl=this.data.requestUrl+"?start="+this.data.totalCount+"&count=20";
    util.http(nextUrl, this.processDoubanData);
    console.log("上滑加载,nextUrl:"+nextUrl);
    wx.showNavigationBarLoading();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /* 跳转到电影详情页面 */
  goToMovieDetail: function (event) {
    var movieId = event.currentTarget.dataset.movieid;
    wx.navigateTo({
        url: "../movie-detail/movie-detail?id="+movieId
    })
    // console.log(event);
},
})